/*
**    Copyright (C) 2003-2011 Institute for Systems Biology
**                            Seattle, Washington, USA.
**
**    This library is free software; you can redistribute it and/or
**    modify it under the terms of the GNU Lesser General Public
**    License as published by the Free Software Foundation; either
**    version 3 of the License, or (at your option) any later version.
**
**    This library is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**    Lesser General Public License for more details.
**
**    You should have received a copy of the GNU Lesser General Public
**    License along with this library; if not, write to the Free Software
**    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

package org.systemsbiology.util;

/**
 * Organization class for static methods for various trivial forms of string manipulation
 * that don't really belong in Main, where they had been.
 * @author anorberg
 *
 */
public class StringUtil {
	
	/**
	 * Causes a runtime exception. Defined for the purpose of blocking any attempt to construct this.
	 * @throws RuntimeException always, since StringUtil is not instantiable.
	 */
	@Deprecated
	private StringUtil(){
        throw new UnsupportedOperationException(
                "StringUtil is a static library that should not be instantiated directly.");
    }

	/**
	 * Glue together the conents of an Object[], separated by a single space.
	 * @param arr Array to glue together.
	 * @return Contents of the array, appended end-to-end with spaces separating them.
	 * Nulls will be represented as "null". Objects will generally be represented
	 * from their toString() methods, as per the behavior of StringBuilder.
	 */
	public static String combine(Object[] arr){
		StringBuilder ret = new StringBuilder();
		for(Object s : arr){
			ret.append(s);
			ret.append(" ");
		}
		ret.deleteCharAt(ret.length() - 1);
		return ret.toString();
	}

    /**
     * Replace URL-encoded %20 with literal spaces
     * @param s {@link String} given for fixup
     * @return {@link String} representation of s, with %20 replaced with a literal space
     * @todo {@priority low} consider a more robust version of this for generic url decoding
     */
	public static String fixSpaces(String s){
		return s.replaceAll(" ", "%20");
	}

    /**
     * Removes all common line terminators from the given string,
     * including carriage return, line feed, and record separator.
     *
     * In most cases, {@link String#trim} is preferable to using this method. This is only useful if you'd like a
     * flattened representation of multiline data without tokenizing the string first.
     * @param s {@link String} given for fixup
     * @return {@link String} representation of s, without carriage return, line feed, or record separator
     */
    public static String removeLineTerminators(String s) {
        return s.replaceAll("[\r\n\u001e]","");
    }
}
